package com.ailink.basic.thread;

public class ObjectWaitTest {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        Thread.dumpStack();

        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                synchronized (lock) {
                    try {
                        System.out.println(Thread.currentThread());
                        Thread.sleep(1000);
                        if (i == 10) {
                            lock.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "wait 1").start();

        Thread.sleep(100);

        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                synchronized (lock) {
                    try {
                        System.out.println(Thread.currentThread());
                        Thread.sleep(1000);
                        if (i == 10) {
                            lock.notify();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "wait 2").start();
    }
}
